package com.authority.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.authority.dao.AuthSysDAO;
import com.authority.dao.AuthSysDomainDAO;
import com.authority.dao.AuthUserSysDAO;
import com.authority.dao.AuthUserDAO;
import com.authority.entry.AuthSysDO;
import com.authority.entry.AuthSysDomainDO;
import com.authority.entry.AuthUserSysDO;
import com.authority.service.AuthSysService;
import com.authority.utils.CommonUtils;
import com.authority.vo.AuthSysVO;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

@Service("authSysService")
@Transactional(rollbackFor = { Exception.class })
public class AuthSysServiceImpl implements AuthSysService {

	@Resource
	AuthSysDAO authSysDAO;

	@Resource
	AuthUserSysDAO authUserSysDAO;

	@Resource
	AuthSysDomainDAO authSysDomainDAO;

	@Resource
	AuthUserDAO userDAO;

	@Override
	public List<AuthSysVO> queryAuthSys() throws Exception {

		List<AuthSysVO> list = Lists.newArrayList();
		List<AuthSysDO> dbList = authSysDAO.queryAuthSys();

		for (AuthSysDO db : dbList) {
			AuthSysVO vo = new AuthSysVO();
			CommonUtils.doTrans(db, vo);
			vo.setOperatorName(userDAO.queryUserNameById(db.getOperatorId())); // 操作人姓名
			list.add(vo);
		}

		return list;
	}

	@Override
	public AuthSysVO queryAuthSysById(Long id) throws Exception {

		AuthSysVO vo = new AuthSysVO();
		AuthSysDO db = authSysDAO.queryAuthSysById(id);
		CommonUtils.doTrans(db, vo);
		vo.setOperatorName(userDAO.queryUserNameById(db.getOperatorId()));// 操作人姓名

		return vo;
	}

	@Override
	public List<AuthSysDomainDO> queryAuthSysDomainBySysId(Long sysId) {

		return authSysDomainDAO.queryAuthSysDomainBySysId(sysId);
	}

	@Override
	public Boolean updateSysInfo(AuthSysVO vo, List<AuthSysDomainDO> list) throws Exception {

		try {
			// 1. 更新系统基本信息
			AuthSysDO db = new AuthSysDO();
			CommonUtils.doTrans(vo, db);
			authSysDAO.updateAuthSys(db);

			// 2. 删除该系统下的所有域名
			authSysDomainDAO.deleteAuthSysDomainBySysId(db.getId());

			// 3. 更新该系统的所有域名
			for (AuthSysDomainDO adb : list) {
				adb.setSysId(db.getId());
			}
			authSysDomainDAO.batchInsertAuthSysDomain(list);
		} catch (Exception e) {
			throw new Exception("系统更新失败");
		}

		return true;
	}

	@Override
	public Boolean insertAuthSys(AuthSysDO db, List<AuthSysDomainDO> list) throws Exception {

		// 判断系统code是否唯一
		AuthSysDO dbCode = authSysDAO.queryAuthSysByCode(db.getCode());
		if (dbCode != null) {
			throw new Exception("系统编码" + db.getCode() + "已经存在");
		}
		try {
			// 1.新增系统信息并取得系统id
			Long sysId = authSysDAO.insertAuthSys(db);

			// 2.循环域名，添加sysId
			for (AuthSysDomainDO adb : list) {
				adb.setSysId(sysId);
			}

			// 3.添加该系统下的域名
			authSysDomainDAO.batchInsertAuthSysDomain(list);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("系统添加失败");
		}

		return true;
	}

	@Override
	public List<AuthSysDO> queryAuthSysByUserId(Long userId) {

		List<AuthSysDO> list = Lists.newArrayList();

		for (AuthUserSysDO db : authUserSysDAO.queryAuthUserSysByUserId(userId)) {
			list.add(authSysDAO.queryAuthSysById(db.getSysId()));
		}

		return list;
	}

	@Override
	public AuthSysDO queryAuthSysByCode(String code) {
		return authSysDAO.queryAuthSysByCode(code);
	}

}
